Conversation
| assert.ok(bytes.length <= 10); | ||
| const storage = new Uint8Array(12); | ||
| storage.set(bytes.subarray(0, 8)); | ||
| storage[8] = bytes[8] ?? 0; |
There was a problem hiding this comment.
I checked this against Swift 6.4.0 wasm32 output, regular and Embedded. _SmallString.capacity is 8 on wasm32; the 10-byte capacity is for 32-bit watchOS. Real 9- and 10-byte literals take the large immortal-string path and decode correctly here.
Could we limit small() and its callers to 8 bytes, keeping the longer inputs in the large-string tests? That would keep the fixtures representative of what Swift actually emits. The decoder's cases 9/10 could then be removed too, but they do not cause a runtime failure today.
There was a problem hiding this comment.
good catch ;)
I added these because there was discussions about re-enabling up to 10 bytes of inline strings again in Swift eventually (forgot where I read it, somewhere sunk in github PRs or issues).
So I figured this would most likely "future proof" the fast decoder.
But you are right, the 9 and 10 decoding path are currently never used as Swift does currently not produce these representations.
should we remove it?
The basic idea is to just pass the 12 byte native representation to JS, allowing for wicked fast small string decoding and "immortal" string caching. Especially for browser use cases, where small ASCII strings are everywhere, this is quite noticeable.
In addition to a tiny bit of added complexity in the glue code, the only real downside is that we add a dependency to Swift's internal String representation, which can of course change with toolchain versions and we'd need to deal with that. I would expect that to be a somewhat infrequent event though.
I know this is a bit wild, but see a few numbers from local benchmarking below - I think it's worth it.
Apple M5 Pro · Node.js 22.21.1 · Local
main(777848df) vs PR implementation. Median milliseconds per 300,000 decodes, across 30 samples. Decoder-only timings; excludes Swift lowering and Wasm calls.abc😄)js-framework-benchmark (in chrome on M5 Pro) with ElementaryUI (current main)
comparing JavaScriptKit 0.59.0 vs this PR
(for reference, shaving off ~10% with other means is getting really, really tough - this is the last big gun I think)
Duration
Memory allocation
Transferred size and first paint